package juc.aqs;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class ReenLockTest {
    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock(true);

        Thread t1 = new Thread(() -> {handle(lock);}, "Thread-1");
        Thread t2 = new Thread(() -> {handle(lock);}, "Thread-2");
        Thread t3 = new Thread(() -> {handle(lock);}, "Thread-3");

        t1.start();
        TimeUnit.SECONDS.sleep(1);
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

    }

    private static void handle(ReentrantLock lock) {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "获取到锁");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } finally {
            lock.unlock();
        }
    }
}
